package com.hhsj.test.redpoint;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.hhsj.redpoint.Redpointable;
import com.hhsj.redpoint.annos.Redpoint;

import java.util.HashSet;
import java.util.Set;

@Redpoint(R.id.content_layout)
public class RedpointalbeLayout2 extends FrameLayout implements Redpointable {

    @Redpoint
    Set<String> redpointTag = new HashSet<>();
    private View redPointView;

    public RedpointalbeLayout2(Context context) {
        this(context, null);
    }

    public RedpointalbeLayout2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RedpointalbeLayout2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        redPointView = new View(context);
//        String idString = this.getClass().getAnnotation(Redpoint.class).viewId();
//        int view_id = getResources().getIdentifier(idString, "id", context.getApplicationContext().getPackageName());
//        redPointView.setId(view_id);
        LayoutParams params = new LayoutParams(10, 10);
        params.setMargins(0, 0, 20, 20);
        params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
        redPointView.setLayoutParams(params);
        redPointView.setBackground(getResources().getDrawable(R.drawable.shape_redpoint));
    }


    @Override
    public void addRedpointTags(String[] tags) {
        if (tags.length > 0) {
            for (String tag : tags) {
                if (!TextUtils.isEmpty(tag) && !redpointTag.contains(tag)) {
                    redpointTag.add(tag);
                }
            }
        }
    }


    @Override
    public void removeRedpointTags(String[] tags) {
        if (tags.length > 0) {
            for (String tag : tags) {
                if (!TextUtils.isEmpty(tag) && redpointTag.contains(tag)) {
                    redpointTag.remove(tag);
                }
            }
        }
    }

    @Override
    public void removeAllRedpointTags() {
        redpointTag.clear();
    }

    @Override
    public void showRedpoint() {
        if (redPointView.getParent() != this) {
            addView(redPointView, getChildCount());
        }
    }

    @Override
    public void removeRedpoint() {
        if (redPointView.getParent() == this) {
            removeView(redPointView);
        }
    }
}
